import java.util.ArrayList;
import java.util.List;

import javax.swing.tree.TreeNode;

/*
 * @lc app=leetcode.cn id=257 lang=java
 *
 * [257] 二叉树的所有路径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<String> res=new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if(root==null){
            return res;
        }
        String path=new String();
        dfs(root,path);
        return res;

    }

    public void dfs(TreeNode root,String path){
        path+=root.val;
        if(root.left==null&&root.right==null){
            res.add(path);
            return;
        }
        if(root.left!=null){
            dfs(root.left,path+"->");//这里隐形回溯了
        }
        if(root.right!=null){
            dfs(root.right,path+"->");//这里隐形回溯了
        }
    }
}
// @lc code=end

